陈庆 14:03:02
<template>
  <div class="dashboard-container">
    <div class="app-container">
      <PageTools>
        <template #before>总共{{ page.total }}条数据</template>
        <el-button type="primary" size="small" @click="$router.push('/import')">导入</el-button>
        <el-button type="primary" size="small" @click="exportData">导出全部</el-button>
        <el-button type="success" size="small">导出选中</el-button>
        <el-button type="primary" size="small" @click="$refs['add-employee'].showDialog=true">新增员工</el-button>
      </PageTools>
      <el-table border :data="list">
        <el-table-column label="序号" sortable="" type="index" />
        <el-table-column label="姓名" sortable="" prop="username" />
        <el-table-column label="头像" sortable="">
          <template slot-scope="{row}">
            <img
              v-immageerror="require('@/assets/common/bigUserHeader.png')"
              style="border-radius: 50%; width: 100px; height: 100px; padding: 10px"
              :src="row.staffPhoto"
              alt=""
              @click="showImg(row.staffPhoto)"
            >
          </template>
        </el-table-column>
        <el-table-column label="工号" sortable="" prop="workNumber" />
        <el-table-column label="聘用形式" sortable="" prop="formOfEmployment" :formatter="formatEmployment" />
        <el-table-column label="部门" sortable="" prop="departmentName" />
        <el-table-column label="入职时间" sortable="" prop="timeOfEntry" />
        <el-table-column label="操作" sortable="" fixed="right" width="280">
          <template slot-scope="{row}">
            <el-button type="text" size="small" @click="$router.push(`/employees/detail/${ row.id}`)">查看</el-button>
            <el-button type="text" size="small">转正</el-button>
            <el-button type="text" size="small">调岗</el-button>
            <el-button type="text" size="small">离职</el-button>
            <el-button type="text" size="small" @click="showDalogSetting(row)">角色</el-button>
            <el-button type="text" size="small" @click="delRow(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <br>
      <!-- 分页器 -->
      <el-pagination
        :current-page.sync="page.page"
        :page-sizes="[2, 3, 4, 10]"
        :page-size="page.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        background
        @size-change="sizeChange"
        @current-change="currentChange"
      />

      <el-row type="flex" justify="center" align="middle" style="height: 60px">
        <el-pagination layout="prev, pager, next" />
      </el-row>
    </div>
    <AddEmployee ref="add-employee" @update-list="getEmployeeList" />
    <!-- 预览图片二维码 -->
    <el-dialog
      title="预览"
      :visible.sync="showCode"
      width="30%"
    >
      <canvas ref="canvas" />
    </el-dialog>
    <AssignRole ref="assign-role" />
  </div>
</template>

<script>
import { getEmployeeList, delEmployee } from '@/api/employees'
import EmployeeEnum from '@/api/constant/employees'
import AddEmployee from './components/add-employee'
import AssignRole from './components/assign-role.vue'
import QRCode from 'qrcode'
export default {
  components: {
    AddEmployee, AssignRole
  },
  data() {
    return {
      loading: false,
      list: [], // 接数据的

      showCode: false,
      page: {
        page: 1, // 当前页码
        size: 10,
        total: 0 // 总数
      }
    }
  },
  created() {
    this.getEmployeeList()
  },
  methods: {
    // 获取列表数据
    async getEmployeeList() {
      const { total, rows } = await getEmployeeList(this.page)
      this.page.total = total
      this.list = rows
    },
    // 每页数量更换
    sizeChange(val) {
      this.page.size = val
      this.getEmployeeList()
    },
    currentChange(val) {
      this.page.page = val
      this.getEmployeeList()
    },
    formatEmployment(row, column, cellValue, index) {
      const result = EmployeeEnum.hireType.find(item => item.id === cellValue * 1)
      return result ? result.value : '未知'
    },
    // 删除员工
    async delRow(id) {
      try {
        await this.$confirm('确定删除吗?')
      } catch (error) {
        return console.log(error)
      }
      // 删除请求
      await delEmployee(id)
      // 成功操作
      this.$message.success('删除成功')
      this.getEmployeeList()
    },
    exportData() {
      const headers = {
        '姓名': 'username',
        '手机号': 'mobile',
        '入职日期': 'timeOfEntry',
        '聘用形式': 'formOfEmployment',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName'
      }
      import('@/vendor/Export2Excel').then(async excel => {
        const { rows } = await getEmployeeList({
          page: 1, // 当前页码
          size: 200000 // 每页数量
        })
        excel.export_json_to_excel({
          header: Object.keys(headers),
          data: this.formatJson(headers, rows),
          multiHeader: [['姓名', '基本信息', '', '', '', '', '部门']],
          merges: ['B1:F1', 'A1:A2', 'G1:G2'],
          filename: '员工资料表',
          autoWidth: true,
          bookType: 'xlsx'
        })
      })
    },
    formatJson(headers, data) {
      console.log(Object.values(headers))
      console.log(data)
      const results = data.map(item => {
        return Object.values(headers).map(key => {
          return item[key]
        })
      })
      console.log(results)
      return results
    },
    // 显示二维码
    showImg(val) {
      if (!val || !val.trim()) return this.$message.warning('暂无图片')
      this.showCode = true
      this.$nextTick(() => {
        QRCode.toCanvas(this.$refs.canvas, val)
      })
    },
    async showDalogSetting(val) {
      await this.$refs['assign-role'].getUserDetailById(val.id)
      this.$refs['assign-role'].showRoleDialog = true
    }
  }
}
</script>

<style>

</style>
